package org.association.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.association.dto.NormalProposalDTO;
import org.association.entity.NormalProposal;
import org.association.entity.UserEntity;
import org.association.entity.VoteRecord;
import org.association.service.NormalProposalService;
import org.association.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "普通提议")
@RestController
@RequestMapping("/normalProposal")
@CrossOrigin
public class NormalProposalController {

    @Autowired
    private NormalProposalService normalProposalService;

    @ApiOperation(value = "获取所有提案")
    @GetMapping("/listProposal")
    public R listProposal(@RequestParam(defaultValue = "001") String organizationID ) {
        ArrayList<NormalProposal> proposals = normalProposalService.getAllProposals(organizationID);
        return R.ok().put("data", proposals);
    }

    @ApiOperation(value = "获取提案下同意的成员")
    @GetMapping("/listAgreePeopleById")
    public R listAgreePeopleById(@RequestParam(defaultValue = "001") String proposalID ) {
        ArrayList<String> users = normalProposalService.getAllAgreePeople(proposalID);
        return R.ok().put("data", users);
    }

    @ApiOperation(value = "获取成员对某个提议的投票情况 1表示同意 0 不同意 -1 放弃")
    @GetMapping("/getVoteRecordById")
    public R getVoteRecordById(@RequestParam(defaultValue = "001") String proposalID, @RequestParam String userID) {
        VoteRecord voteRecord = normalProposalService.getVoteRecordById(proposalID, userID);
        return R.ok().put("data", voteRecord);
    }


    @ApiOperation(value = "增加提案")
    @PostMapping("/addProposal")
    public R addProposal(@RequestBody NormalProposalDTO normalProposalDTO){
        NormalProposal proposal = normalProposalService.addNormalProposalDTO(normalProposalDTO);
        return R.ok().put("data", proposal);
    }

    @ApiOperation(value = "给一个提案投票")
    @PostMapping("/voteOneProposal")
    public R voteOneProposal(@RequestParam String proposalID, @RequestParam String userID, @RequestParam Integer isSupport) {
        Integer stat = normalProposalService.voteOneProposal(proposalID, userID, isSupport);
        NormalProposal normalProposal = normalProposalService.getOneProposalByID(proposalID);
        if (stat == -1){
            return R.error().put("data", normalProposal);
        }else{
            return R.ok().put("data", normalProposal);
        }
    }

}
